package com.dalitek.sampleface.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dalitek.sampleface.R;
import com.oliveapp.face.libfacecapture.AFace;
import com.oliveapp.face.libfacecapture.x.entity.Face;

import java.io.File;
import java.util.List;

public  class FaceQueryAdapter extends BaseAdapter {
    private Context context;
    private List<Face> faceList;
    private AFace aFace;
    private AlertDialog alertDialog;
    private int captureType;
    public FaceQueryAdapter(Context context, List<Face> faceList, AFace aFace, AlertDialog alertDialog,int  captureType) {
        this.context = context;
        this.faceList = faceList;
        this.aFace = aFace;
        this.alertDialog = alertDialog;
        this.captureType = captureType;
    }

    @Override
    public int getCount() {
        return faceList.size();
    }

    @Override
    public Object getItem(int i) {
        return faceList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if(view == null){
            holder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.item_dialog_query_list,null);
            holder.ivFace = view.findViewById(R.id.iv_face);
            holder.tvUserId = view.findViewById(R.id.tv_user_id);
            holder.tvUserName = view.findViewById(R.id.tv_user_name);
            holder.tvDelete = view.findViewById(R.id.tv_delete);
            view.setTag(holder);
        }else{
            holder = (ViewHolder) view.getTag();
        }
        Face face = faceList.get(i);
        holder.tvUserId.setText(face.getUserid());
        holder.tvUserName.setText(face.getUsername());
        holder.tvDelete.setOnClickListener(view1 -> {
            if(captureType>0){//1v1 or 1vn
                if(this.aFace.delFace(face.getUserid())) {
                    toast("删除成功");
                    faceList.remove(face);
                    this.notifyDataSetChanged();
                    alertDialog.setTitle("人员列表"+"("+faceList.size()+")");
                }else{
                    toast("删除失败");
                }
            }else{
                this.aFace.clearIdCardFace();
                faceList.remove(face);
                this.notifyDataSetChanged();
                toast("删除成功!");
                alertDialog.setTitle("人员列表"+"("+0+")");
            }

        });
        if(face.getFace()!=null){
            switch (face.getFacetype()){
                case 1://本地路径
                    File file = new File(face.getFace());
                    if (file.exists()) {
                        holder.ivFace.setImageBitmap(BitmapFactory.decodeFile(face.getFace()));
                    }
                    break;
            }
        }
        return view;
    }

    class ViewHolder{
        ImageView ivFace;
        TextView tvUserId;
        TextView tvUserName;
        TextView tvDelete;
    }
    private void toast(String text){
        Toast.makeText(context,text,Toast.LENGTH_SHORT).show();
    }
}
